package com.qdlc.p2p.manage.action.additional;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.BaseGiveAwardService;
import com.qdlc.p2p.biz.service.RedEnvelopeService;
import com.qdlc.p2p.biz.service.UserRedEnvelopeService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.UserRedEnvelopeModel;

/**
 * 用户红包管理
 * 
 * @author ylx
 * @version 2.0
 * @date 2015年5月16日 下午2:57:12
 */
public class ManageUserRedEnvelopeAction extends BaseAction<UserRedEnvelopeModel> implements
		ModelDriven<UserRedEnvelopeModel> {
	@Resource
	private  RedEnvelopeService redEnvelopeService;
	@Resource
	private UserRedEnvelopeService userRedEnvelopeService;

	@Resource
	@Qualifier("giveUserRedEnvelopeService")
	BaseGiveAwardService giveUserRedEnvelopeService;
	
	private UserRedEnvelopeModel model = new UserRedEnvelopeModel();
	
	public UserRedEnvelopeModel getModel() {
		
		return model;
	}
	
	private Map<String, Object> data = new HashMap<String, Object>();
	
	/**
	 * 用户红包管理 页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/additional/redEnvelope/userRedEnvelope/userRedEnvelopeManager")
	public String userRedEnvelopeManager() throws Exception {
		return "userRedEnvelopeManager";
	}
	
	/**
	 * 用户红包列表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/additional/redEnvelope/userRedEnvelope/userRedEnvelopeList")
	public void userRedEnvelopeList() throws Exception {
		// 初始化status值为99
		String status = paramString("status");
		if (StringUtil.isBlank(status)) 
			model.setStatus((byte) 99);
		
		PageDataList<UserRedEnvelopeModel> pageDataList = userRedEnvelopeService.list(model);
		int total = pageDataList.getPage().getTotal();
		data.put("total", total);
		data.put("rows", pageDataList.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 导出用户红包报表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/additional/redEnvelope/userRedEnvelope/exportExcelUserRedEnvelopeList")
	public void exportExcelUserRedEnvelopeList() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		model.setPage(1);
		model.setRows(5000);
		// 获取记录
		PageDataList<UserRedEnvelopeModel> pageDataList = userRedEnvelopeService.list(model);
		
		String title = "用户红包获取记录Excel表";
		
		String[] hearders = new String[] { "编号", "用户名", "红包名称", "红包金额","红包类型", "状态", "有效天数","过期时间","使用时间","获取时间", "红包来源", "使用类型" };// 表头数组
		String[] fields = new String[] { "id", "userName", "name", "amount", "typeStr", "statusStr", "validDay", "expiredTime","useTime","addTime","sourceType","useTypeStr"};// 对象属性数组
		TableData td = ExcelUtils.createTableData(pageDataList.getList(), ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}
	
	/**
	 * 置用户红包为无效
	 * 
	 * @throws Exception
	 */
	@Action("/modules/additional/redEnvelope/userRedEnvelope/doUserRedEnvelopeFailure")
	public void doUserRedEnvelopeFailure() throws Exception {
		userRedEnvelopeService.doInvalid(model.getId());
		printResult(MessageUtil.getMessage("MS100002001"), true);
	}
	/**
	 * 发放红包页面
	 */
	@Action("/modules/additional/redEnvelope/userRedEnvelope/append")
	public String append(){
		request.setAttribute("usableredEnvelopeTypeList", redEnvelopeService.findUsableRedEnvelope());
		return "append";
	}
	/**
	 * 用户添加红包
	 */
	@Action("/modules/additional/redEnvelope/doAppend")
	public void doAppend() throws Exception{
		data = new HashMap<String, Object>();
		//后台无法给用户名<4的用户发红包
		//model.validModelData();
		giveUserRedEnvelopeService.giveAward(model.prototypeForAppend());
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS100001001"));
		printJson(getStringOfJpaObj(data));
	}
}
